package com.tree.backend.controller;

import com.tree.backend.common.Result;
import com.tree.backend.entity.dto.budgetConfig.BudgetConfigDTO;
import com.tree.backend.entity.dto.budgetConfig.BudgetConfigQueryDTO;
import com.tree.backend.entity.vo.budgetConfig.BudgetAnalysisVO;
import com.tree.backend.entity.vo.budgetConfig.BudgetConfigVO;
import com.tree.backend.service.IBudgetConfigService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 部门预算配置表 前端控制器
 * </p>
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/budget-configs")
public class BudgetConfigController {

    private final IBudgetConfigService budgetConfigService;

    /**
     * 获取预算配置列表
     */
    @GetMapping
    public Result<?> getBudgetConfigs(BudgetConfigQueryDTO queryDTO) {
        return Result.success(budgetConfigService.getBudgetConfigs(queryDTO));
    }

    /**
     * 获取预算配置详情
     */
    @GetMapping("/{id}")
    public Result<?> getBudgetConfigById(@PathVariable Long id) {
        BudgetConfigVO budgetConfigVO = budgetConfigService.getBudgetConfigById(id);
        if (budgetConfigVO == null) {
            return Result.error(404, "预算配置不存在");
        }
        return Result.success(budgetConfigVO);
    }

    /**
     * 新增预算配置
     */
    @PostMapping
    public Result<?> createBudgetConfig(@RequestBody BudgetConfigDTO budgetConfigDTO) {
        BudgetConfigVO budgetConfigVO = budgetConfigService.createBudgetConfig(budgetConfigDTO);
        return Result.success(budgetConfigVO);
    }

    /**
     * 更新预算配置
     */
    @PutMapping("/{id}")
    public Result<?> updateBudgetConfig(@PathVariable Long id, @RequestBody BudgetConfigDTO budgetConfigDTO) {
        BudgetConfigVO budgetConfigVO = budgetConfigService.updateBudgetConfig(id, budgetConfigDTO);
        if (budgetConfigVO == null) {
            return Result.error(404, "预算配置不存在");
        }
        return Result.success(budgetConfigVO);
    }

    /**
     * 删除预算配置
     */
    @DeleteMapping("/{id}")
    public Result<?> deleteBudgetConfig(@PathVariable Long id) {
        budgetConfigService.deleteBudgetConfig(id);
        return Result.success(null);
    }

    /**
     * 获取预算分析数据
     * 为前端柱状图提供数据支持，包含部门预算分布和成本项目预算分布
     */
    @GetMapping("/analysis")
    public Result<BudgetAnalysisVO> getBudgetAnalysis(@RequestParam(required = false) Integer year) {
        try {
            // 如果未提供年份，使用当前年份
            if (year == null) {
                year = java.time.Year.now().getValue();
            }
            
            BudgetAnalysisVO analysisVO = budgetConfigService.getBudgetAnalysis(year);
            
            // 确保返回的数据不为null
            if (analysisVO == null) {
                analysisVO = new BudgetAnalysisVO();
                analysisVO.setDepartmentAnalysis(new java.util.ArrayList<>());
                analysisVO.setCostItemAnalysis(new java.util.ArrayList<>());
            }
            
            return Result.success(analysisVO);
        } catch (Exception e) {
            return Result.error(500, "获取预算分析数据失败：" + e.getMessage());
        }
    }
}
